<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
      <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
      <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <!--      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>-->
      <!--      <el-popconfirm-->
      <!--          class="ml-5"-->
      <!--          confirm-button-text='确定'-->
      <!--          cancel-button-text='我再想想'-->
      <!--          icon="el-icon-info"-->
      <!--          icon-color="red"-->
      <!--          title="您确定批量删除这些数据吗？"-->
      <!--          @confirm="delBatch">-->
      <!--        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>-->
      <!--      </el-popconfirm>-->
      <!--      &lt;!&ndash; <el-upload action="http://localhost:9090/judge/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">-->
      <!--        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>-->
      <!--      </el-upload>-->
      <!--      <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button> &ndash;&gt;-->
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
      <el-table-column prop="id" label="ID" width="100" v-if="false"></el-table-column>
<!--      <el-table-column prop="name" label="姓名" align="center" ></el-table-column>-->
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">
          <div style="display: inline-block">
            <el-image style="width: 60px; border-radius: 50%;
                        position: relative;height: 60px" :src="scope.row.avatar"
                      :preview-src-list="[scope.row.avatar]"></el-image>
            <span style="position:relative; bottom:13px;margin-left: 14px">{{ scope.row.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="sex"  align="center" :formatter="sexFormatter"
                       label="性别"></el-table-column>
      <el-table-column prop="idcard"  align="center" label="身份证号"></el-table-column>
      <el-table-column prop="phone" align="center"  label="手机"></el-table-column>

      <el-table-column label="操作" width="200px"  align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="查看培训记录" placement="top-start">
            <el-button type="text" @click="queryRecord(scope.row)"><i class="el-icon el-icon-view"></i>
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[10, 30, 50, 100]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>
    </div>

    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="40%" :close-on-click-modal="false">
      <el-form label-width="120px" size="small" style="width: 80%; margin: 0 auto">
        <el-form-item prop="name" label="姓名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="sex" label="性别">
          <el-input v-model="form.sex" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="idcard" label="身份证号">
          <el-input v-model="form.idcard" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="phone" label="手机">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="avatar" label="证件照">
          <el-upload action="http://localhost:9090/file/upload" ref="img"
                     :on-success="handleImgUploadSuccess">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item prop="representUnit" label="代表单位">
          <el-input v-model="form.representUnit" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="workUnit" label="工作单位">
          <el-input v-model="form.workUnit" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="judgeLevel" label="现裁判等级">
          <el-input v-model="form.judgeLevel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="passDate" label="现等级通过时间">
          <el-date-picker v-model="form.passDate" type="date" value-format="yyyy-MM-dd"
                          placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item prop="latestRecord" label="最近执裁或复训活动名称及地点">
          <el-input v-model="form.latestRecord" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

<!--    <el-dialog title="执裁记录" :visible.sync="dialogTableVisible">-->
<!--      <el-table :data="recordData">-->
<!--        <el-table-column property="raceName" label="赛事名称" width="150"></el-table-column>-->
<!--        <el-table-column property="raceStation" label="分站" width="200"></el-table-column>-->
<!--        <el-table-column property="raceLevel" label="赛事级别"></el-table-column>-->
<!--        <el-table-column property="racePost" :formatter="postFormatter" label="担任职位"></el-table-column>-->
<!--      </el-table>-->
<!--    </el-dialog>-->

  </div>
</template>

<script>
export default {
  name: "Coach",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
    const data = {
            "page":this.pageNum,
            "pageSize":this.pageSize,
            "name":this.name
          }
      this.request.post("/coach/page", data).then(res => {
        //this.tableData = res.data.records
        this.tableData = res.data.data
        this.total = res.data.total
      })
    },
    sexFormatter(row) {
      if (row.sex === "0") {
        return "男"
      } else if (row.sex === "1") {
        return "女"
      }
    },
    save() {
      this.request.post("/coach/save", this.form).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message)
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.file) {
          this.$refs.file.clearFiles();
         }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
       this.$nextTick(() => {
         if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.file) {
          this.$refs.file.clearFiles();
         }
       })
    },
    del(id) {
      this.request.delete("/coach/" + id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择需要删除的数据")
        return
      }
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/coach/del/batch", ids).then(res => {
        if (res.code === 200) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    download(url) {
      window.open(url)
    },
    exp() {
      window.open("http://localhost:9090/coach/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    }
  }
}
</script>


<style>
.headerBg {
  background: #eee!important;
}

.el-button {
  margin: 0px 6px 12px;
}

.el-icon {
  font-size: 16px;
}
</style>
